//
//  OneLevelBaseViewController.m
//  Inforcollection
//
//  Created by macbook on 15/12/2.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "OneLevelBaseViewController.h"
#import "BaseTabBarController.h"
#import "PersonCenterView.h"
#import "PersonalSettingViewController.h"
#import "UserData.h"


@implementation OneLevelBaseViewController
{
    PersonCenterView *personView;
    UIAlertView*LoginAlert;
}
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviBarLeftBtn:nil];

    //添加个人按钮
    [self addPersonButton];
    //添加侧滑边框
    [self addSideBar];
    
    [SharedNotifiCenter addObserver:self selector:@selector(LoginSuccessYesOrNo) name:isLoginSuccess object:nil];
    
    [self LoginSuccessYesOrNo];
}
-(void)LoginSuccessYesOrNo
{
    if ([UserData shareUserData].isLogin)
    {
        _isLogin=YES;
        [personView reloadDataWithUserModel:[UserData shareUserData].userModel WithLogin:_isLogin];
    }
    else
    {
        _isLogin=NO;
        [personView reloadDataWithUserModel:[UserData shareUserData].userModel WithLogin:_isLogin];
    }
}
//添加个人按钮
-(void)addPersonButton
{
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"ico_user"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(show:) forControlEvents:UIControlEventTouchUpInside];
    [self setNaviBarLeftBtn:button];
}
//点击按钮触发显示侧滑边框
-(void)show:(UIButton *)button
{
    [self.rightSideBar show];
}
//添加侧滑边框
-(void)addSideBar
{
    self.rightSideBar = [[CDRTranslucentSideBar alloc]init];
    self.rightSideBar.delegate = self;
//    self.rightSideBar.translucentStyle = UIBarStyleBlack;
    self.rightSideBar.tag = 0;
    _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self.view addGestureRecognizer:_panGestureRecognizer];
    
    personView = [PersonCenterView setFrame:CGRectMake(0, 0, self.rightSideBar.sideBarWidth, ScreenHeight) WithModel:[UserData shareUserData].userModel WihtLogin:[UserData shareUserData].isLogin WithCallBack:^(NSIndexPath *selectIndex)
    {
        if(selectIndex.section==0 && _isLogin)
        {
            return ;
        }
        
        [self.rightSideBar dismiss];
        
        if(!_isLogin)//设置未登录全部跳转到登陆页面
        {
            [self toLoginVC];
        }
        else//登录了的状态
        {
            
            if (selectIndex.section==0)
            {
//                [self toPersonalSettingViewController];
            }
            else{
                switch (selectIndex.row) {
                    case 0:
                        [self.tabBarController setSelectedIndex:0];

                           break;
                    case 1:
                        [self.tabBarController setSelectedIndex:1];
                        break;
                    case 2:
                        [self.tabBarController setSelectedIndex:2];
                        break;
//                    case 3:
//                        [self pushToMyConsultingVC];
//                        break;
//                    case 4:
//                        [self pushToMyComplaintsVC];
//                        break;
//                    case 5:
//                        [self pushToSetting];
//                        break;
                    default:
                       LoginAlert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"你确认要注销当前账号吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
                        LoginAlert.tag=255;
                        [LoginAlert show];
                        
                        break;
                }
            }
        }
    }];
    [self.rightSideBar setContentViewInSideBar:personView];
}

- (void)jumpToPerson
{
    PersonCenterViewController *personVC = [[PersonCenterViewController alloc] init];

    personVC.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:personVC animated:YES];
    
}


#pragma mark-------侧滑边框实现-------------------------------------------
//手势显示侧滑边框
#pragma mark - Gesture Handler
- (void)handlePanGesture:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {
        CGPoint startPoint = [recognizer locationInView:self.view];
        
        // Left SideBar
        if (startPoint.x < self.view.bounds.size.width / 3.0) {
            self.rightSideBar.isCurrentPanGestureTarget = YES;
        }
        // Right SideBar
        else {
//            self.rightSideBar.isCurrentPanGestureTarget = YES;
        }
    }
//    self.rightSideBar.isCurrentPanGestureTarget=NO;
    
    [self.rightSideBar handlePanGestureToShow:recognizer inView:self.view];
}
#pragma mark - CDRTranslucentSideBarDelegate
- (void)sideBar:(CDRTranslucentSideBar *)sideBar didAppear:(BOOL)animated
{
    if (sideBar.tag == 0) {
//        NSLog(@"Left SideBar did appear");
    }
    
    if (sideBar.tag == 1) {
//        NSLog(@"Right SideBar did appear");
    }
}

- (void)sideBar:(CDRTranslucentSideBar *)sideBar willAppear:(BOOL)animated
{
    if (sideBar.tag == 0) {
//        NSLog(@"Left SideBar will appear");
    }
    
    if (sideBar.tag == 1) {
        
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
        
//        NSLog(@"Right SideBar will appear");
    }
}

- (void)sideBar:(CDRTranslucentSideBar *)sideBar didDisappear:(BOOL)animated
{
    if (sideBar.tag == 0) {
//        NSLog(@"Left SideBar did disappear");
    }
    
    if (sideBar.tag == 1) {
        
        self.rightSideBar.view.backgroundColor=[UIColor clearColor];
        
//        NSLog(@"Right SideBar did disappear");
    }
}

- (void)sideBar:(CDRTranslucentSideBar *)sideBar willDisappear:(BOOL)animated
{
    if (sideBar.tag == 0) {
//        NSLog(@"Left SideBar will disappear");
    }
    
    if (sideBar.tag == 1) {
        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];

//        NSLog(@"Right SideBar will disappear");
    }
}

#pragma mark-------UIAlert Delegate

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   
        if (buttonIndex==1)
        {
            [[UserData shareUserData] loginOut];
            
            [self toLoginVC];
            
            [self.tabBarController setSelectedIndex:0];
            
            [SharedNotifiCenter postNotificationName:isLoginSuccess object:nil];
        }

    
   }

//跳转到个人中心界面
-(void)toPersonalSettingViewController
{
    PersonalSettingViewController *personalSetting=[self.storyboard instantiateViewControllerWithIdentifier:@"PersonalSettingViewController"];
    
    [self.navigationController pushViewController:personalSetting animated:YES];
}
-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end
